<include file="public@header"/>
<link href="/themes/admin_simpleboot3/public/assets/themes/flatadmin/modal.css" rel="stylesheet" type="text/css">
<script type="text/html" id="photos-item-tpl">
    <li id="saved-image{id}">
        <input id="photo-{id}" type="hidden" name="photo_urls[]" value="{filepath}">
        <input class="" id="photo-{id}-name" type="text" name="photo_names[]" value="{name}" title="图片名称" style="width:200px;">
   
        <img id="photo-{id}-preview" src="{url}" onclick="imagePreviewDialog(this.src);">
        <a href="javascript:uploadOneImage('{:lang('UPLOAD_IMAGE')}','#photo-{id}');">{:lang('REPLACE')}</a>
        <a href="javascript:(function(){$('#saved-image{id}').remove();})();">{:lang('REMOVE')}</a>
    </li>
</script>
<script type="text/html" id="files-item-tpl">
    <li id="saved-file{id}">
        <input id="file-{id}" type="hidden" name="file_urls[]" value="{filepath}">
        <input class="layui-input" id="file-{id}-name" type="text" name="file_names[]" value="{name}"
               style="width: 200px;" title="文件名称">
        <a id="file-{id}-preview" href="{preview_url}" target="_blank">下载</a>
        <a href="javascript:uploadOne('图片上传','#file-{id}','file');">替换</a>
        <a href="javascript:(function(){$('#saved-file{id}').remove();})();">移除</a>
    </li>
</script>
</head>
<body>
<div class="layui-fluid js-check-wrap">
    

    <form action="{:url('AdminGoods/addPost')}" method="post" class="layui-form js-ajax-form" name="theForm" id="theForm"> 
        <div class="smart-widget">
            <div class="layui-card">
                <ul class="layui-tab-title">
                    <li class="active">
                        <a href="#A" data-toggle="tab">
                            <span class="icon-wrapper"><i class="fa fa-align-center"></i></span>
                            <span class="text-wrapper">{:lang('GOODS_INFO')}</span>
                        </a>
                    </li>
                    <li>
                        <a href="#B" data-toggle="tab">
                            <span class="icon-wrapper"><i class="fa fa-cog"></i></span>
                            <span class="text-wrapper">{:lang('GOODS_ATTR')}</span>
                        </a>
                    </li>
                    <li>
                        <a href="#C" data-toggle="tab">
                            <span class="icon-wrapper"><i class="fa fa-cog"></i></span>
                            <span class="text-wrapper">{:lang('SEO_OPT')}</span>
                        </a>
                    </li>
                </ul>
                <div class="layui-card-body">
                    <div class="tab-content">
                        <div class="layui-tab-item layui-show">
                            <div class="layui-card-body">
                                <!-- 产品名称 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('GOODS_NAME')}</label>
                                    <div class="layui-input-block">
                                        <input class="layui-input" type="text" name="post[goods_name]" id="goods_name"
                                                placeholder="{:lang('PLEASE_ENTER')}{:lang('GOODS_NAME')}"/>
                                    </div>
                                </div>

                                <!-- 产品分类 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('GOODS_CATEGORY')}</label>
                                    <div class="layui-input-block">
                                        <!-- 分类多选 -->
                                        <!-- <input class="layui-input" type="text"
                                               value="{$category['name']}"
                                               placeholder="{:lang('PLEASE_SELECTED_CATEGORY')}" onclick="doSelectCategory();" id="js-categories-name-input"
                                               readonly/>
                                        <input class="layui-input" type="hidden" value="{$category['id']}"
                                               name="post[categories]"
                                               id="js-categories-id-input"/> -->

                                        <!-- 分类单选 -->
                                        <select class="layui-input" name="post[categories]">
                                            <option value=''>{:lang('SELECT_PLEASE')}</option>
                                            {$category_tree|default=''}
                                        </select>
                                    </div>
                                </div>

                                <!-- 发布时间 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('ADD_TIME')}</label>
                                    <div class="layui-input-block">
                                        <div class="input-group">
                                            <input class="layui-input js-layui-datetime" type="text" name="post[published_time]"
                                                   value="{:date('Y-m-d H:i:s',time())}" readonly>
                                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                        </div>
                                    </div>
                                </div>

                                <!-- 产品简介 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('GOODS_EXCERPT')}</label>
                                    <div class="layui-input-block">
                                        <textarea class="layui-input" name="post[post_excerpt]" rows="5" id="post_excerpt" placeholder=" {:lang('PLEASE_FILL_IN')}{:lang('GOODS_EXCERPT')}"></textarea>
                                    </div>
                                </div>

                                <!-- 详细内容 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('DETAIL_CONTENT')}</label>
                                    <div class="layui-input-block">
                                        <script type="text/plain" id="content" name="post[post_content]"></script>
                                    </div>
                                </div>

                                <!-- 上传图片 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('UPLOAD_IMAGE')}</label>
                                
                                    <div class="layui-input-block">
                                        <div class="upload-image">
                                            <input type="hidden" name="post[more][thumbnail]" id="thumbnail"
                                                   value="">
                                            <a href="javascript:uploadOneImage('{:lang('UPLOAD_IMAGE')}','#thumbnail');">
                                                <img src="__TMPL__/public/assets/images/default-thumbnail.png"
                                                     id="thumbnail-preview"
                                                     width="150"/>
                                            </a>
                                            <input type="button" class="layui-btn layui-btn-danger layui-btn-sm btn-cancel-thumbnail" value="{:lang('CANCEL_PICTURE')}">
                                        </div>
                                    </div>
                                </div>

                                <!-- 图片推荐尺寸 -->
                                <!-- <notempty name="category['id']">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">{:lang('PICTURE_RECOMMENDED_SIZE')}</label>
                                        <div class="col-lg-5">
                                            <table class="thumb-size">
                                                <tr>
                                                    <td>{:lang('CATEGORY')}</td>
                                                    <td>{:lang('IMAGE_SIZE')}</td>
                                                </tr>
                                                <tr>
                                                    <td>{$category.name}</td>
                                                    <td>{$category.thumb_size}</td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>
                                </notempty> -->
                                
                                <!-- 相册 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('ALBUM')}</label>
                                    <div class="layui-input-block">
                                        <ul id="photos" class="pic-list list-unstyled form-inline"></ul>
                                        <a href="javascript:uploadMultiImage('{:lang("UPLOAD_IMAGE")}','#photos','photos-item-tpl');"
                                           class="layui-btn layui-btn-normal">{:lang('SELECT_PICTURE')}</a>
                                    </div>
                                </div>

                                <!-- <tr>
                                    <th>产品备注</th>
                                    <td>
                                        <input class="layui-input" type="text" attrs="remarks" name="post[remarks]"
                                               id="remarks" value="" placeholder="请输入产品备注"/>
                                    </td>
                                </tr>
                                <tr>
                                    <th>技术特点</th>
                                    <td>
                                        <textarea class="layui-input" id="charac" name="post[charac]" style="height: 60px;" placeholder="技术特点"></textarea>
                                    </td>
                                </tr>
                                <tr>
                                    <th>转向链接</th>
                                    <td>
                                        <input class="layui-input" type="text" name="post[href]" id="href" placeholder="http://">
                                        <p class="layui-word-aux">可跳转到指定地址</p>
                                    </td>
                                </tr> -->
                                
                                <!-- 属性 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('ATTRIBUTE')}</label>
                                    <div class="layui-input-block">
                                        <div class="checkbox">
                                            <div class="custom-checkbox">
                                                <input type="checkbox" id="is_top" name="post[is_top]" value="1">
                                                <label for="is_top"></label>
                                            </div>
                                            <div class="inline-block vertical-top">
                                                {:lang('IS_TOP')}&nbsp;&nbsp;<!--是否最新-->
                                            </div>

                                            <div class="custom-checkbox">
                                                <input type="checkbox" id="recommended" name="post[recommended]" value="1">
                                                <label for="recommended"></label>
                                            </div>
                                            <div class="inline-block vertical-top">
                                                {:lang('RECOMMENDED')}&nbsp;&nbsp;<!--是否推荐-->
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <!-- 模版 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('GOODS_TEMPLATE')}</label>
                                
                                    <div class="layui-input-block">
                                        <select class="layui-input" name="post[more][template]" id="more-template-select">
                                            <foreach name="goods_theme_files" item="vo">
                                                <php>$value=preg_replace('/^goods\//','',$vo['file']);</php>
                                                <option value="{$value}">{$vo.name} {$vo.file}.html</option>
                                            </foreach>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>
    
                        <div class="tab-pane fade in" id="B" style="padding:15px 0 35px;">
                            <!-- <div class="smart-widget-body" style="padding-bottom:20px;"> -->
                                <table class="goodsAttr table">
                                    <tr>
                                        <th class="control-label col-lg-2" style="border:0;">{:lang('GOODS_ADMINGOODSTYPE_INDEX')}</th>
                                        <td class="col-lg-10" style="border:0;">
                                            <select class="layui-input" name="post[goods_type]" onchange='getAttrList("{:url('AdminGoods/getAttr')}")'>
                                                <option value="0">{:lang('SEL_GOODS_TYPE')}</option>
                                                {$goods_type_list}
                                            </select>
                                            <p class="layui-word-aux">{:lang('notice_goods_type')}</p>
                                        </td>
                                    </tr>
                                </table>
                                <div id="tbody-goodsAttr">{$goods_attr_html}</div>
                            <!-- </div> -->
                        </div>

                        <div class="tab-pane fade in" id="C">
                            <div class="layui-card-body">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('PAGE_TITLE')}</label>
                                    <div class="layui-input-block">
                                        <input class="layui-input" type="text" name="post[page_title]" id="keywords"
                                               placeholder="{:lang('PLEASE_ENTER')}{:lang('PAGE_TITLE')}">
                                    </div>
                                </div>

                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('PAGE_KEYWORDS')}</label>
                                    <div class="layui-input-block">
                                        <input class="layui-input" type="text" name="post[page_keywords]" id="keywords" 
                                               placeholder="{:lang('PLEASE_ENTER')}{:lang('PAGE_KEYWORDS')}">
                                    </div>
                                </div>

                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('PAGE_DESCRIPTION')}</label>
                                    <div class="layui-input-block">
                                        <textarea class="layui-input" name="post[page_description]" rows="5" 
                                                  placeholder="{:lang('PLEASE_FILL_IN')}{:lang('PAGE_DESCRIPTION')}"></textarea>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="layui-input-block">
                        <button type="submit" class="layui-btn layui-btn-normal js-ajax-submit" style="display:none;" id="js-ajax-submit">{:lang('ADD')}</button>
                        <button type="button" class="layui-btn layui-btn-normal" onclick="CheckSubmit();" id="add-key">{:lang('ADD')}</button>
                        <!-- <button type="button" class="layui-btn layui-btn-normal content-preview" onclick="contentPreview(editorcontent,'goods_name');" id="add-key">{:lang('PREVIEW')}</button> -->
                        <a class="layui-btn layui-btn-primary" href="javascript:history.back(-1);">{:lang('BACK')}</a>
                    </div>
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
    </form>

</div>

<include file="public@footer"/>

<script type="text/javascript" src="__STATIC__/js/transport.js"></script>
<script type="text/javascript" src="__STATIC__/js/modal.js"></script>
<script type="text/javascript" src="__STATIC__/js/utils.js"></script>
<script type="text/javascript">
    //编辑器路径定义
    var editorURL = GV.WEB_ROOT;
</script>
<script type="text/javascript" src="__STATIC__/js/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="__STATIC__/js/ueditor/ueditor.all.js"></script>


<script type="text/javascript">
    $(function () {
        if( $('#content').length ){
            $('.content-preview').show();
            UE.getEditor('content');
        }

        //移除缩略图
        $('.btn-cancel-thumbnail').click(function () {
            $('#thumbnail-preview').attr('src', '__TMPL__/public/assets/images/default-thumbnail.png');
            $('#thumbnail').val('');
        });

    });

    // 选择分类
    function doSelectCategory() {
        var selectedCategoriesId = $('#js-categories-id-input').val();
        openIframeLayer("{:url('AdminGoodsCategory/select')}?operate=add&ids=" + selectedCategoriesId, '请选择分类', {
            area: ['700px', '400px'],
            btn: ['确定', '取消'],
            yes: function (index, layero) {
                //do something

                var iframeWin          = window[layero.find('iframe')[0]['name']];
                var selectedCategories = iframeWin.confirm();
                if (selectedCategories.selectedCategoriesId.length == 0) {
                    layer.msg('请选择分类');
                    return;
                }
                $('#js-categories-id-input').val(selectedCategories.selectedCategoriesId.join(','));
                $('#js-categories-name-input').val(selectedCategories.selectedCategoriesName.join(' '));
                //console.log(layer.getFrameIndex(index));
                layer.close(index); //如果设定了yes回调，需进行手工关闭
            }
        });
    }

    function CheckSubmit(){
        var goods_name   = $("#goods_name").val();
        // var remarks      = $("#remarks").val();
        // var keywords     = $("#keywords").val();
        var post_excerpt = $("#post_excerpt").val();
        // var charac       = $("#charac").val();
        var content      = editorcontent.getPlainTxt();
        var href = "{:url('AdminGoods/replace')}";
        // var data = {'data':'GOODS_NAME:'+goods_name+',REMARKS:'+remarks+',KEYWORDS:'+keywords+',POSTEXCERPT:'+post_excerpt+',CHARAC:'+charac+',CONTENT:'+content};
        var data = {'data':'GOODS_NAME:'+goods_name+',POSTEXCERPT:'+post_excerpt+',CONTENT:'+content};
        $.ajax({
            url:  href,
            data: data,
            type: 'post',
            dataType: 'json',
            beforeSend:function(){
            },
            success: function (res) {
                if(res[0] !=1){
                    var str = '';
                    for(var p in res){
                        str = str+res[p];
                    }
                    str = str+'<p style="color:red;">是否确认替换？</p>';
                    $("#add-key").createModal({
                        background: "#000",//设定弹窗之后的覆盖层的颜色
                        width: "500px",//设定弹窗的宽度
                        height: "auto",//设定弹窗的高度
                        resizable: true,//设定弹窗是否可以拖动改变大小
                        move: false,//规定弹窗是否可以拖动
                        bgClose: false,//规定点击背景是否可以关闭
                        html: "<div class='modal-promot-mess'>提示信息</div>"
                                +"<div class='messge'>"+str+"</div>"
                                 +"<p class='insure-btn-con'><span class='sure-btn' onclick='tijiao()'>确定</span><span class='cancel-btn modal-close'>取消</span></p>",
                        
                    });
                }else{
                    $('#js-ajax-submit').click();
                }
            }
        });
    }
    function tijiao(){
        $('#js-ajax-submit').click();
    };

    /**
     * 新增一个规格
     */
    function addSpec(obj)
    {
        var src   = obj.parentNode.parentNode;
        var idx   = rowindex(src);
        var tbl   = document.getElementById('attrTable');
        var row   = tbl.insertRow(idx + 1);
        var cell1 = row.insertCell(-1);
        var cell2 = row.insertCell(-1);
        var regx  = /<a([^>]+)<\/a>/i;

        cell1.className = 'th-label';
        cell1.innerHTML = src.childNodes[0].innerHTML.replace(/(.*)(addSpec)(.*)(\[)(\+)/i, "$1removeSpec$3$4-");
        cell2.innerHTML = src.childNodes[1].innerHTML.replace(/readOnly([^\s|>]*)/i, '');
    }

    /**
     * 删除规格值
     */
    function removeSpec(obj)
    {
        var row = rowindex(obj.parentNode.parentNode);
        var tbl = document.getElementById('attrTable');

        tbl.deleteRow(row);
    }
</script>
